package cn.com.dhcc.turbo.dao.registry;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.dhcc.app.core.CM;
import cn.com.dhcc.app.core.dao.AbstractCrudDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.wapper.DomainInfoRowWapper;
import cn.com.dhcc.turbo.entity.registry.wapper.NodeInfoRowWapper;

/**
 * 业务域管理dao
 * 
 * @日期：2014-5-15 下午4:16:46
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Repository
public class DomainInfoDao extends AbstractCrudDao<DomainInfo> {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	/**
	 * 获取节点业务域 分页
	 * 
	 * @param pager
	 * @param condition
	 * @return
	 * @throws DaoException
	 */
	public PagerData<DomainInfo> list(Pager pager, WhereCondition condition)
			throws DaoException {
		String msg = "";
		String sql = "select t1.*,t2.node_name from " + SI_TABLE("TURBO_DOMAIN_INFO")+"  t1"
							+" left join " + SI_TABLE("turbo_node_info")+" t2"
							+" on t1.node_id = t2.id ";

		try {
			return CM.getDao().getPagerList("获取节点业务域信息列表", sql,
					new DomainInfoRowWapper(), pager, condition,
					new Object[] {});
		} catch (DaoException e) {
			msg = "获取节点业务域列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 获取节点业务域信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getDomainByNodeId(String nodeId) throws DaoException {
		String msg = "";
		String sql = "select * from " + SI_TABLE("turbo_domain_info")+" where node_id=? and status='1'";
		try {
			return CM.getDao().list("获取节点业务域信息列表", sql,
					new DomainInfoRowWapper(),new Object[] {nodeId});
		} catch (DaoException e) {
			msg = "获取节点业务域列表失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 获取业务域
	 * @param id
	 * @return 
	 * @throws DaoException
	 */
	public DomainInfo get(String id) throws DaoException {
		String msg = "";
		String sql = "select t1.*,t2.node_name from " + SI_TABLE("turbo_domain_info")+"  t1"
							+" left join " + SI_TABLE("turbo_node_info")+" t2"
					       +"      on t1.node_id = t2.id "
							+" where t1.id=? and t1.status='1'";
		try {
			return CM.getDao().get(" 获取业务域信息", sql,new DomainInfoRowWapper(),new Object[] {id});
		} catch (DaoException e) {
			msg = " 获取业务域失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	
	/**
	 * 获取本节点业务域
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getLocalDomain() throws DaoException {
		String msg = "";
		String sql = "		select t1.* from " + SI_TABLE("turbo_domain_info")+"  t1"
							+" 	where  t1.status='1'";
		try {
			return CM.getDao().list(" 获取本节点业务域信息", sql,new DomainInfoRowWapper(),new Object[] {});
		} catch (DaoException e) {
			msg = " 获取本节点业务域失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	/**
	 * 获取节点业务域信息
	 * @param nodeId
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getAll() throws DaoException {
		String sql = "select * from " + SI_TABLE("turbo_domain_info") +" where status='1'";
		try {
			return CM.getDao().list("获取节点业务域信息列表", sql,
					new DomainInfoRowWapper(),new Object[] {});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	public void del(String id) throws DaoException {
		String sql = "update " + SI_TABLE("turbo_domain_info") + " set status='0' where id=? ";
		try {
			CM.getDao().update("删除业务域", sql, new Object[] { id});
		} catch (DaoException e) {
			throw new DaoException(e);
		}
	}
	@SuppressWarnings("rawtypes")
	public boolean isPropertyUnique(String property, String newValue,String oldValue) throws DaoException {
		if (newValue.equals(oldValue)) {
			return true;
		}
		final String sql = "select * from " + SI_TABLE("turbo_domain_info")
				+ " where  " + property + "=? and status=1";
		try {
			List list = CM.getDao().list("验证唯一", sql, new NodeInfoRowWapper(),
					new Object[] {newValue });
			if (list.size() == 0)
				return true;
		} catch (DaoException e) {
			throw new DaoException("验证唯一失败！", e);
		}
		return false;
	}
	public String MaxCode() throws DaoException {
		try {
			final String sql = "select " + "   max(code)" + " code "
					+ " from " + SI_TABLE("turbo_domain_info");
			return  CM.getDao().queryForString("获取最大节点编码", sql,new Object[] {  }) ;
		} catch (DaoException e) {
			throw new DaoException("获取最大节点编码失败！", e);
		}
	}
	public List<DomainInfo> getAllForDataCategory() throws DaoException {
		String msg = "";
		String sql = "		select t1.id,t1.name,count(t2.id) cont from " + SI_TABLE("turbo_domain_info")+"  t1 left join " + SI_TABLE("turbo_data_category")+" t2 on t1.id = t2.pid"
							+" 	where  t1.status='1' group by t1.id,t1.name,t1.status";
		try {
			return CM.getDao().list(" 获取本节点业务域信息", sql,new DomainInfoRowWapper(),new Object[] {});
		} catch (DaoException e) {
			msg = " 获取本节点业务域失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
}
